package com.parking.dao.impl;

import com.parking.bean.SystemAdmin;
import com.parking.dao.SystemAdminDao;
import com.parking.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SystemAdminDaoImpl implements SystemAdminDao {
    @Override
    public SystemAdmin findByUsername(String username) {
        /*链接数据库*/
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet=null;
        try {
            /*获取链接*/
            conn = DBUtil.getConnection();
            /*获取预编译的数据库操作对象*/
            String sql = "select * from user where username=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            /*执行Sql语句*/
            resultSet = ps.executeQuery();
            /*处理结果集*/
            if (resultSet.next()) {
                String id =resultSet.getString(1);
                 username =resultSet.getString(2);
                String password =resultSet.getString(3);
                String name =resultSet.getString(4);
                String telephone =resultSet.getString(5);
                return new SystemAdmin(id,username,password,name,telephone);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                /*释放资源*/
                DBUtil.close(conn, ps, null);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return null;
    }
}
